//package com.openvas.core.gas.service.impl;
//
//import com.coconet.gas.constant.dwEnum;
//import com.coconet.gas.dao.IpsRuleMapper;
//import com.coconet.gas.dao.ReportMapper;
//import com.coconet.gas.dao.TargetIpMapper;
//import com.coconet.gas.dao.TaskIpMapper;
//import com.coconet.gas.exception.ServiceException;
//import com.coconet.gas.service.TaskService;
//import com.coconet.gas.vo.dw.IpsRule;
//import com.coconet.gas.vo.dw.Report;
//import com.coconet.gas.vo.dw.TaskIp;
//import com.coconet.gas.vo.request.PortListVo;
//import com.gxa.entity.TargetEntity;
//import com.gxa.entity.TaskEntity;
//import com.gxa.entity.TaskInfoEntity;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import javax.annotation.Resource;
//import java.util.Collections;
//import java.util.List;
//import java.util.Map;
//
//@Service
//public class TaskServiceImpl implements TaskService {
//    @Resource
//    private TaskIpMapper taskIpMapper;
//    @Resource
//    private TargetIpMapper targetIpMapper;
//    @Resource
//    private IpsRuleMapper ipsRuleMapper;
//    @Resource
//    private OpenvasTool2 openvasTool;
//    @Resource
//    private ReportMapper reportMapper;
//    //private OpenvasTool2 openvasTool = new OpenvasTool2();
//
//    @Override
//    @Transactional
//    public List<Report> getReportByTaskId(String taskId) throws Exception {
//        String taskName = null;
//        List<TaskInfoEntity> tasksList = openvasTool.getTasksList();
//        for (TaskInfoEntity task : tasksList) {
//            if (task.getTaskId().equals(taskId)) {
//                taskName = task.getName();
//            }
//        }
//        TaskIp taskIp = new TaskIp();
//        taskIp.setTaskId(taskId);
//        TaskIp taskIp1 = taskIpMapper.selectOne(taskIp);
//        if (taskIp1 == null) {
//            throw new ServiceException(dwEnum.FAILURE.code(), "扫描报告已过期,请重新开始扫描");
//        } else if (taskIp1.getReportId() == null) {
//            throw new ServiceException(dwEnum.FAILURE.code(), "扫描报告已过期,请重新开始扫描");
//        }
//
//        Report report = new Report();
//        report.setTaskId(taskId);
//        if (reportMapper.select(report) == null || reportMapper.select(report).size() == 0) {
//            Map<String, Object> resultMap = openvasTool.getReport(taskIp1.getReportId());
//            List<Map<String, Object>> results = (List<Map<String, Object>>) resultMap.get("results");
//            for (Map<String, Object> result : results) {
//                String resultId = (String) result.get("resultId");
//                //resultIdList.add(resultId);
//                Map<String, Object> reportResult = openvasTool.getReportResult(resultId);
//                String cve = (String) reportResult.get("cve");
//                String qod = (String) reportResult.get("qod");
//                IpsRule ipsRule = new IpsRule();
//                ipsRule.setCve(cve);
//                List<IpsRule> ipsRuleList = ipsRuleMapper.select(ipsRule);
//                String cveName = null;
//                if (ipsRuleList != null && ipsRuleList.size() > 0) {
//                    cveName = ipsRuleMapper.select(ipsRule).get(0).getRname();
//                } else {
//                    cveName = (String) reportResult.get("name");
//                }
//                Report report1 = new Report();
//                report1.setTaskId(taskId);
//                if (cve.equals("NOCVE")) {
//                    report1.setCve("无CVE编号");
//                } else {
//                    report1.setCve(cve);
//                }
//                report1.setSeverity(result.get("severity").toString());
//                report1.setQod(qod);
//                report1.setCveName(cveName);
//                report1.setThreat(result.get("threat").toString());
//                report1.setTaskName(taskName);
//                reportMapper.insertSelective(report1);
//                //gasReport.setLevel(ipsRule1.getLevel());
//                //gasReport.setLevel(result.get("threat").toString());
//                //gasReport.setSeverity(result.get("severity").toString());
//                //gasReport.setQod(qod);
//                //gasReport.setCveName(cveName);
//            }
//            /*TaskReport taskReport = new TaskReport();
//            taskReport.setTaskId(taskId);
//            taskReport.setGasReportList(gasReportList);
//            taskReportList.add(taskReport);*/
//        }
//        List<Report> reportList = reportMapper.select(report);
//        Collections.sort(reportList, (p1, p2) -> Double.valueOf(p2.getSeverity()).compareTo(Double.valueOf(p1.getSeverity())));
//        return reportMapper.select(report);
//    }
//
//
//    /**
//     * 创建端口集合
//     *
//     * @param portListVo
//     * @throws Exception
//     */
//    @Override
//    public void createPortList(PortListVo portListVo) throws Exception {
//        List<Map<String, Object>> portList = openvasTool.getOnePortListByPortName(portListVo.getName());
//        if (portList!=null && !portList.isEmpty()) {{
//            for(Map<String,Object> map:portList){
//                if(map.get("name").equals(portListVo.getName())){
//                    throw new ServiceException(dwEnum.FAILURE.code(), "该端口集合已存在,请确认后重新添加");
//                }
//            }
//        }
//        }
//
//        openvasTool.createPortList(portListVo.getName(), portListVo.getComment(), portListVo.getPort_range());
//
//    }
//
//    @Override
//    public void createTarget(TargetEntity target) throws Exception {
//        for (TargetEntity targetExist : openvasTool.getTargetList()) {
//            if (targetExist.getName().equals(target.getName())) {
//                throw new ServiceException(dwEnum.FAILURE.code(), "该扫描目标已存在,请确认后重新添加");
//            }
//        }
//        target.setReverse_lookup_only("0");
//        target.setReverse_lookup_unify("0");
//        target.setAlive_tests("Scan Config Default");
//        openvasTool.createTarget(target);
//
//    }
//
//    @Override
//    @Transactional
//    public void createTask(TaskEntity taskEntity) throws Exception {
//        String taskId = null;
//        List<TaskInfoEntity> tasksList = openvasTool.getTasksList();
//        for (TaskInfoEntity task : tasksList) {
//            if (task.getName().equals(taskEntity.getName())) {
//                throw new ServiceException(dwEnum.FAILURE.code(), "该任务已存在,请确认后重新添加");
//            }
//        }
//        openvasTool.createTask(taskEntity);
//        tasksList = openvasTool.getTasksList();
//        for (TaskInfoEntity task : tasksList) {
//            if (task.getName().equals(taskEntity.getName())) {
//                taskId = task.getTaskId();
//            }
//        }
//        String reportId = openvasTool.startTask(taskId);
//        TaskIp taskIp = new TaskIp();
//        taskIp.setTaskId(taskId);
//        taskIp.setReportId(reportId);
//        taskIpMapper.insertSelective(taskIp);
//    }
//
//    @Override
//    @Transactional
//    public void startTask(String taskId) {
//        String reportId = null;
//        try {
//            reportId = openvasTool.startTask(taskId);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        TaskIp taskIp = new TaskIp();
//        taskIp.setTaskId(taskId);
//        TaskIp taskIp1 = taskIpMapper.selectOne(taskIp);
//        taskIp.setReportId(reportId);
//        if (taskIp1 != null) {
//            taskIp.setId(taskIp1.getId());
//            taskIpMapper.updateByPrimaryKeySelective(taskIp);
//        } else {
//            taskIpMapper.insertSelective(taskIp);
//        }
//        Report report = new Report();
//        report.setTaskId(taskId);
//        reportMapper.delete(report);
//    }
//
//    @Override
//    @Transactional
//    public void deleteTask(String taskId) {
//        try {
//            openvasTool.deleteTask(taskId);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        Report report = new Report();
//        report.setTaskId(taskId);
//        reportMapper.delete(report);
//        TaskIp taskIp = new TaskIp();
//        taskIp.setTaskId(taskId);
//        taskIpMapper.delete(taskIp);
//    }
//
//
//}
